<template>
<div class="login">
<van-button
plain
type="primary"
block
size="large"
@click=userLogin
>微信用户一键登录</van-button>
</div>
</template>

<script>
	import Dialog from "@/../static/vant/dialog/dialog";
	import store from "@/utils/store.js";
	import { host } from "@/utils/index.js";

	var hasData = false;

	App({
		onLaunch: function (options) {
			var extraData = null;
			if (options.referrerInfo.extraData) {
				extraData = options.referrerInfo.extraData;
				hasData = true;
				console.log(extraData);
			}
		},
	});

	export default {
		config: {
			navigationBarTitleText: "用户登录",
		},
		data() {
			return {
				user: { nickName: "立即登录" },
			};
		},
		methods: {
			userLogin: function () {
				wx.login({
					success(res) {
						if ("login:ok" == res.errMsg) {
							wx.request({
								url: host + "/wechat/login?code=" + res.code,
								header: { "content-type": "application/json" },
								method: "GET",
								//complete: function (data) {
								//	console.log(data);
								//},
								success: function (data) {
									var result = data.data;
									if (result.code == 20000) {
										store.commit("setToken", result.data.token);
										//弹出获取用户信息
										wx.getUserInfo({
											success: function (res1) {
												if (res1.errMsg == "getUserInfo:ok") {
													//获取用户信息失败
													wx.request({
														url: host + "/wechat/perfect/info/",
														header: { Authorization: result.data.token },
														method: "POST",
														data: {
															sessionKey: result.data.sessionKey,
															signature: res1.signature,
															rawData: res1.rawData,
															encryptedData: res1.encryptedData,
															iv: res1.iv,
														},
														success: function (res2) {

															if (res2.mobile != "") {
																store.commit("setUser", res2.data.data);
																const url = '../../index/index'
                                                                wx.navigateBack({url})
															} else {
																Dialog.alert({
																	title: "小提示！",
																	message: "登录失败,获取不到用户信息！",
																});
															}
														},
													});
												}
											},
										});
									} else {
										if (data.statusCode == 200) {
											Dialog.alert({
												title: "小提示！",
												message: "登录失败！" + result.msg,
											});
										} else {
											Dialog.alert({
												title: "小提示！",
												message: "系统异常！" + result.msg,
											});
										}
									}
								},
							});
						} else {
							Dialog.alert({
								title: "小提示！",
								message: "登录失败！" + res.errMsg,
							});
						}
					},
				});
			},
		},
	};
</script>


<style>
	.login {
		margin: 5px;
		text-align: center;
	}
	.login-account {
		padding-top: 15px;
	}
</style>
